#include "cards.h"

Cards::Cards() {}

Cards::Cards(const Card &card)
{
    add(card);
}

void Cards::add(const Card &card)//增加一张
{
    m_cards.insert(card);
}

void Cards::add(const Cards &cards)//添加多张，添加进一个集合
{
    m_cards.unite(cards.m_cards);
}

void Cards::add(const QVector<Cards> &cards)
{
    for(int i=0;i<cards.count();i++)
    {
        add(cards.at(i));
    }
}

Cards &Cards::operator <<(const Card &card)
{
    add(card);
    return *this;
}

Cards &Cards::operator <<(const Cards &cards)
{
    add(cards);
    return *this;
}



void Cards::remove(const Card &card)//删除单张
{
    m_cards.remove(card);
}

void Cards::remove(const Cards &cards)//删除多张，删除一个集合
{
    m_cards.subtract(cards.m_cards);
}

void Cards::remove(const QVector<Cards> &cards)
{
    for(int i=0;i<cards.size();i++)
    {
        remove(cards.at(i));
    }
}

int Cards::cardCount()
{
    return m_cards.size();
}

bool Cards::isEmpty()
{
    return m_cards.isEmpty();
}

void Cards::clear()
{
    m_cards.clear();
}

Card::CardPoint Cards::maxPoint()
{
    Card::CardPoint max=Card::Point_Begin;
    if(!m_cards.isEmpty())
    {
        for(auto it=m_cards.begin();it!=m_cards.end();it++)
        {
            if(it->point()>max)
            {
                max=it->point();
            }
        }
    }
    return max;

}

Card::CardPoint Cards::minPoint()
{
    Card::CardPoint min=Card::Point_End;
    if(!m_cards.isEmpty())
    {
        for(auto it=m_cards.begin();it!=m_cards.end();it++)
        {
            if(it->point()<min)
            {
                min=it->point();
            }
        }
    }
    return min;
}

int Cards::pointCount(Card::CardPoint point)
{
    int count=0;
    for(auto it=m_cards.begin();it!=m_cards.end();it++)
    {
        if(it->point()==point)
        {
            count++;
        }
    }
    return count;
}

bool Cards::contains(const Card &card)
{
    return m_cards.contains(card);//contains可以在set容器中查询某个元素，像find
}

bool Cards::contains(const Cards &cards)
{
    return m_cards.contains(cards.m_cards);
}

Card Cards::takeRandCard()
{
    //生成一个随机数
    int num=QRandomGenerator::global()->bounded(m_cards.size());//QRandomGenerator::global() 返回一个全局共享的随机数生成器实例，bounded(int) 方法生成一个[0,m_cards.size())的随机数
    QSet<Card>::const_iterator it=m_cards.constBegin();//const_iterator是只读迭代器
    for(int i=0;i<num;i++,++it);
    Card card=*it;
    m_cards.erase(it);
    return card;
}

CardList Cards::toCardList(SortType type)
{
    CardList list;
    for(auto it=m_cards.begin();it!=m_cards.end();it++)
    {
        list<<*it;
    }
    if(type==Asc)
    {
        std::sort(list.begin(),list.end(),lessSort);
    }
    else if(type==Desc)
    {
        std::sort(list.begin(),list.end(),greaterSort);
    }
    return list;
}
